//: src/com/silvtar/net/ftp/SSFtpClient.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 6/4/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 6/ 4/2013    0.1        Create the class.
 */
package com.silvtar.net.transporter;

import java.io.IOException;
import java.util.LinkedList;

import com.silvtar.io.StdOut;
import com.silvtar.sync.model.SSAnalyzer;
import com.silvtar.sync.model.SSException;
import com.silvtar.sync.model.SSTreeNode;

/**
 * 文件树传输类
 * 本类使用内置文件传输器进行文件传输
 * 
 * @author silvtar
 * @author piaosn@gmail.com
 * @version 0.2
 */
public class FileTreeTransporter extends Thread{
    
    static int threadNumber;
    private FileTransporter        fileTransporter;
    private String                 localRoot; // 本地根目录
    private String                 remoteRoot; // 远程根目录
    LinkedList<SSTreeNode> outQueue;
    LinkedList<SSTreeNode> inQueue;
    
    // ---------------------------- 构造方法 ---------------------------
    /**
     * 传输器通过分析器获得源地址以及目的地址
     * 
     * @param analyzer
     *            分析器
     * @param localRoot
     *            本地同步根目录
     * @param remoteRoot
     *            远程同步根目录
     */
    public FileTreeTransporter(String ipAddr, int commandPort, int dataPort) {
        setName("FileTreeTransporter");
        fileTransporter = new FileTransporter(ipAddr, commandPort, dataPort);
    }
    
    
    /**
     * 开始传输
     * 
     * @throws SSException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public void startTransport(SSAnalyzer analyzer, String localRoot, String remoteRoot){
        
        outQueue = (LinkedList<SSTreeNode>) analyzer.getOutQueue().clone();
        inQueue = (LinkedList<SSTreeNode>) analyzer.getInQueue().clone();
        this.localRoot = localRoot;
        this.remoteRoot = remoteRoot;
        
        start();
    }
    
    /**
     * 上传文件节点
     * 
     * @param currentFile_TN
     *            要上传的文件节点
     */
    private void upload(SSTreeNode currentFile_TN) {
        String localFile_S = localRoot + currentFile_TN.getRelativePath();
        String remoteFile_S = remoteRoot + currentFile_TN.getRelativePath();

        fileTransporter.uploadFolder(localFile_S, remoteFile_S);
    }
    
    /**
     * 下载文件节点
     * 
     * @param currentFile_TN
     *            要上传的文件节点
     */
    private void download(SSTreeNode currentFile_TN) {
        String localFile_S = localRoot + currentFile_TN.getRelativePath();
        String remoteFile_S = remoteRoot + currentFile_TN.getRelativePath();

        fileTransporter.downloadFolder(remoteFile_S, localFile_S);
    }
    
    public void run() {
        setName("FileTreeTransporter" + threadNumber);
        fileTransporter.startDataServer();
        
        // 从local传输到remote
        while (!outQueue.isEmpty()) {
            SSTreeNode currentTreeNode = outQueue.pop();
            upload(currentTreeNode);
        }

        // 从remote传输到local
        while (!inQueue.isEmpty()) {
            SSTreeNode currentTreeNode = inQueue.pop();
            download(currentTreeNode);
        }
        StdOut.println("传输完毕");
        
        fileTransporter.closeDataServer();
    }
    
    public void closeFileTransporter() {
        fileTransporter.closeDataServer();
    }
}
